/*
 * EmpiricalFormula.java
 * 
 * Generates the empirical formula based on composition % of each element in the compound.
 * 
 * Author: Andres Ruiz
 * License: see License.txt
 */
public class EmpiricalFormula
{
	private double[] ElementMoles;
	private double[] ElementAmount;
	private String[] Elements;
	private double lowestMole;
	private String LowestMoleName;
	PeriodicTableAcess info;
	InfoStringParse infoParse;

	public EmpiricalFormula(double[] elementmoles, String[] elements)
	{
		ElementMoles = elementmoles;
		Elements = elements;

		for(int i = 0; i < Elements.length; i++)//get the correct name from the database
		{
			 info = new PeriodicTableAcess(Elements[i]);
			 infoParse = new InfoStringParse(info.getSearchResult());
			 Elements[i] = infoParse.getName();
		}
		lowestMole();
		EmpiricalCacl();
	}
	
	public void lowestMole()
	{
		lowestMole = ElementMoles[0];// set element at index 0 as the default
		LowestMoleName = Elements[0];
		for(int i = 0; i < ElementMoles.length; i++)
		{
			if(lowestMole > ElementMoles[i])
			{
				lowestMole = ElementMoles[i];
				LowestMoleName = Elements[i];
			}
		}
	}
	public void EmpiricalCacl()
	{
		ElementAmount = new double[ElementMoles.length];
		for(int i =0; i < ElementMoles.length; i++)
		{
			ElementAmount[i] = ElementMoles[i]/lowestMole;
		}
	}
	
	public void printResults()
	{
		String moleOf = " Moles Of: ";
		System.out.println("These results are aproximates.");
		System.out.println("Please round the to get the actual empiric formula.");
		for(int i = 0; i< ElementAmount.length; i++)
		{
			System.out.println(ElementMoles[i] + moleOf + Elements[i]+ " Divided By");
			System.out.println(lowestMole + moleOf + LowestMoleName);
			//System.out.println("For a subscript of: " + ElementAmount[i]);
			System.out.println();
			System.out.println(ElementMoles[i] +" / " + lowestMole + " = " + ElementAmount[i]);
			System.out.println();
		}
	}
	
	public double[] getResults()
	{
		return ElementAmount;
	}
}
